package com.threeonemeeting.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;

public class WebViewActivity extends BaseTabActivity {

	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_oauth_webview);
		init("微博授权登录");

		showLoadingLayout("正在打开微博登录页面，请稍候");

		WebView wv = (WebView) findViewById(R.id.web);
		wv.setWebViewClient(new WebViewClient() {

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String strUrl) {

				String str = strUrl.substring(0, 15);
				if (str != null) {
					if (str.compareTo("weiboandroidsdk") == 0) {

						String strHost = strUrl.substring(18);
						int nIndex = strHost.indexOf('?');
						if (nIndex == -1)
							return false;

						strHost = strHost.substring(0, nIndex);

						// 新浪微博
						Intent intent = new Intent(WebViewActivity.this,
								MainActivity.class);
						Uri uri = Uri.parse(strUrl);
						intent.setData(uri);
						intent.putExtra("dest", "WeiboCallbackActivity");
						if (strHost.compareTo("weibo_host") == 0) {
							intent.putExtra("from", "sina");
						} else if (strHost.compareTo("qweibo_host") == 0) {
							intent.putExtra("from", "qq");
						} else {
							return false;
						}

						startActivity(intent);
						return false;
					}
				}

				view.loadUrl(strUrl);
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);

				showWebView();
			}
		});

		Intent i = this.getIntent();
		if (!i.equals(null)) {
			Bundle b = i.getExtras();
			if (b != null) {
				if (b.containsKey("url")) {
					String url = b.getString("url");
					wv.getSettings().setJavaScriptEnabled(true);
					wv.getSettings().setSupportZoom(true);
					wv.getSettings().setBuiltInZoomControls(true);
					wv.loadUrl(url);
					wv.requestFocus();
				}
			}
		}
	}

	private void showLoadingLayout(String strHint) {

		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		ScrollView webViewLayout = (ScrollView) findViewById(R.id.layout_webview);

		if (loadingLayout != null) {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		if (webViewLayout != null) {
			webViewLayout.setVisibility(View.GONE);
		}

		TextView loadingText = (TextView) findViewById(R.id.loading_text);
		if (loadingText != null) {
			loadingText.setText(strHint);
		}
	}

	private void showWebView() {
		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		ScrollView webViewLayout = (ScrollView) findViewById(R.id.layout_webview);

		if (loadingLayout != null) {
			loadingLayout.setVisibility(View.GONE);
		}

		if (webViewLayout != null) {
			webViewLayout.setVisibility(View.VISIBLE);
		}
	}
}
